// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.property;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.dengues.commons.ui.swt.table.AbstractTableViewer;
import org.dengues.core.components.NodePropertyParameter;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class PropertyTableViewer extends AbstractTableViewer {

    private final NodePropertyParameter param;

    private PropertyTableProvider provider;

    private CommandStack commandStack;

    /**
     * Qiang.Zhang.Adolf@gmail.com PropertyTableViewer constructor comment.
     * 
     * @param parent
     * @param style
     * @param param
     * @param commandStack
     */
    public PropertyTableViewer(Composite parent, NodePropertyParameter param, boolean showToolBar) {
        super(parent, param.isReadOnly(), showToolBar);
        this.param = param;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "initTable".
     */
    @Override
    protected void initTable() {
        super.initTable();
    }

    /**
     * Getter for tableViewer.
     * 
     * @return the tableViewer
     */
    @Override
    public TableViewer getTableViewer() {
        return this.tableViewer;
    }

    /**
     * Getter for table.
     * 
     * @return the table
     */
    public Table getTable() {
        return this.table;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstactTableViewer#configAddButtonListener()
     */
    @Override
    public SelectionListener configAddButtonListener() {
        return new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                Object object = tableViewer.getInput();
                ArrayList<Map<String, Object>> arrayList = (ArrayList<Map<String, Object>>) object;
                String[] itemNames = param.getItemNames();
                Object[] itemValues = param.getItemValues();
                Map<String, Object> map = new HashMap<String, Object>();
                for (int i = 0; i < itemNames.length; i++) {
                    String string = itemNames[i];
                    NodePropertyParameter parameter = (NodePropertyParameter) itemValues[i];
                    map.put(string, parameter.getValue()); //$NON-NLS-1$
                }
                arrayList.add(map);
                if (commandStack != null) {
                    commandStack.execute(new Command() {
                    });
                }
                tableViewer.refresh();
            }
        };
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstactTableViewer#attachCellEidtor()
     */
    @Override
    public void attachCellEidtor() {
        TableColumn column;
        Object[] itemValues = param.getItemValues();
        String[] itemNames = param.getItemDisplayNames();
        CellEditor[] cellEditors = new CellEditor[itemValues.length + 1];
        String[] propertyIDs = new String[itemValues.length + 1];
        TextCellEditor cellEditor3 = new TextCellEditor(table);
        cellEditors[0] = cellEditor3;
        propertyIDs[0] = SPACES;
        for (int i = 0; i < itemNames.length; i++) {
            String string = itemNames[i];
            column = new TableColumn(table, SWT.NONE);
            column.setText(string);
            column.setWidth(100);
            Object object = itemValues[i];
            if (object instanceof NodePropertyParameter) {
                NodePropertyParameter parameter = (NodePropertyParameter) object;
                EParameterFieldCategory fieldCategory = parameter.getFieldCategory();
                switch (fieldCategory) {
                case COMBO:
                    ComboBoxCellEditor cellEditor1 = new ComboBoxCellEditor(table, NodeParameterUtils.getDisplayItems(parameter));
                    propertyIDs[i + 1] = param.getItemNames()[i];
                    cellEditors[i + 1] = cellEditor1;
                    break;
                case CHECK:
                    CheckboxCellEditor cellEditor = new CheckboxCellEditor(table);
                    propertyIDs[i + 1] = param.getItemNames()[i];
                    cellEditor.setValue(parameter.getValue());
                    cellEditors[i + 1] = cellEditor;
                    break;
                default:
                    TextCellEditor cellEditor2 = new TextCellEditor(table);
                    propertyIDs[i + 1] = param.getItemNames()[i];
                    cellEditor2.setValue(parameter.getValue());
                    cellEditors[i + 1] = cellEditor2;
                    break;
                }
            }
        }
        tableViewer.setColumnProperties(propertyIDs);
        tableViewer.setCellEditors(cellEditors);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstactTableViewer#attachProvider()
     */
    @Override
    public void attachProvider() {
        provider = new PropertyTableProvider();
        tableViewer.setContentProvider(provider);
        tableViewer.setLabelProvider(provider);

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstactTableViewer#initTableInput()
     */
    @Override
    public void initTableInput() {
        super.initTableInput();
        provider.setParam(param);
        tableViewer.setInput(param.getValue());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstactTableViewer#configRemoveButtonListener()
     */
    @Override
    public SelectionListener configRemoveButtonListener() {
        return new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                Object object = tableViewer.getInput();
                ArrayList<Map<String, Object>> arrayList = (ArrayList<Map<String, Object>>) object;
                TableItem[] selection = table.getSelection();
                for (TableItem tableItem : selection) {
                    arrayList.remove(tableItem.getData());
                }
                if (commandStack != null) {
                    commandStack.execute(new Command() {
                    });
                }
                tableViewer.refresh();
            }
        };
    }

    /**
     * Getter for param.
     * 
     * @return the param
     */
    public NodePropertyParameter getParam() {
        return this.param;
    }

    /**
     * Sets the commandStack.
     * 
     * @param commandStack the commandStack to set
     */
    public void setCommandStack(CommandStack commandStack) {
        this.commandStack = commandStack;
    }
}
